/*
 * AGENT SHERPA
 *  
 * Assignement 2 of the Agent Oriented Software Engineering Course
 * M.Sc. in Advanced Software Engineering (Part-time)
 * University College Dublin www.ucd.ie
 *
 * Copyright (c) 2007 Chris Walsh, Aidan Morrisy and Iain Hull.
 * All rights reserved.
 * 
 * Filename: Main.aps
 * Author:   iain
 * Date:     06 February 2008
 */

package org.agentsherpa.model;

/**
 * A Cell location on the map
 * 
 * @author iain
 */
public class Point {
    public static final Point BadLocation = new Point(-1, 1);

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    @Override
    public String toString() {
        return "(" + x + ", " + y + ")";
    }
    
    @Override
    public boolean equals(Object rhs) {
        if (rhs instanceof Point) {
            return equals((Point) rhs);
        }
        return false;
    }

    public boolean equals(Point rhs) {
        return x == rhs.x && y == rhs.y;
    }
    
    @Override
    public int hashCode() {
        return x * 1234567 + y;
    }
    
    protected int x;

    protected int y;

    /**
     * Calculate the direction tartget is from the origin
     * 
     * @param origin
     * @param target
     * @return
     */
	public static Direction quadrant(Point origin, Point target) {
		Point p = target.minus(origin);
		
		if (p.getX() >=0 && p.getY() >= 0) {
			// Top right corner
			if (p.getX() > p.getY()) {
				return Direction.East;
			}
			else {
				return Direction.North;
			}
		}
		else if (p.getX() >= 0 && p.getY() < 0) {
			// Bottom right corner
			if (p.getX() > p.getY()) {
				return Direction.East;
			}
			else {
				return Direction.South;
			}
		}
		else if (p.getX() < 0 && p.getY() < 0) {
			// Bottom left corner
			if (p.getX() < p.getY()) {
				return Direction.West;
			}
			else {
				return Direction.South;
			}
		}
		else if (p.getX() < 0 && p.getY() >= 0) {
			// Top left corner
			if (p.getX() < p.getY()) {
				return Direction.West;
			}
			else {
				return Direction.North;
			}
		}
		return Direction.North;
	}

	private Point minus(Point p) {
		return new Point(x - p.x, y-p.y);
	}

	public double distance(Point p) {
		int dx = x - p.x;
		int dy = y - p.y;
		
		return Math.sqrt((dx * dx) + (dy * dy));
	}
}